package org.dromara.knowledgebase.controller;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.knowledgebase.core.page.PageQuery;
import org.dromara.knowledgebase.core.page.TableDataInfo;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledgebase.domain.Knowledge;
import org.dromara.knowledgebase.domain.bo.KnowledgeBo;
import org.dromara.knowledgebase.service.KnowledgeService;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库管理
 *
 * @author xiaog
 * @date 2025-05-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/knowledge")
public class KnowledgeController extends BaseController {

    private final KnowledgeService knowledgeService;

    /**
     * 查询知识库管理列表
     */
//    @SaCheckPermission("system:knowledge:list")
    @GetMapping("/list")
    public TableDataInfo<Knowledge> list(@Validated(QueryGroup.class) KnowledgeBo bo, PageQuery pageQuery) {
        return knowledgeService.selectPageKnowledgeList(bo, pageQuery);
    }

    /**
     * 获取知识库管理详细信息
     *
     * @param documentId 主键
     */
//    @SaCheckPermission("system:knowledge:query")
    @GetMapping("/{documentId}")
    public R<Knowledge> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long documentId) {
        return R.ok(knowledgeService.queryById(documentId).orElse(null));
    }

    /**
     * 新增知识库管理
     */
//    @SaCheckPermission("system:knowledge:add")
    @Log(title = "知识库管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Knowledge> add(@Validated(AddGroup.class) @RequestBody KnowledgeBo bo) {
        return R.ok(knowledgeService.insertByBo(bo));
    }

    /**
     * 修改知识库管理
     */
//    @SaCheckPermission("system:knowledge:edit")
    @Log(title = "知识库管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<UpdateResponse.Result> edit(@Validated(EditGroup.class) @RequestBody KnowledgeBo bo) {
        return R.ok(knowledgeService.updateByBo(bo));
    }

    /**
     * 删除知识库管理
     *
     * @param documentIds 主键串
     */
//    @SaCheckPermission("system:knowledge:remove")
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{documentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] documentIds) {
        knowledgeService.deleteByIds(List.of(documentIds));
        return toAjax(documentIds.length);
    }
}
